<?php

namespace common\modules\user\models\frontend;

use common\modules\user\Module;
use Yii;

/**
 * Class User
 *
 * @property string  $username        Username
 * @property string  $email           E-mail
 * @property string  $password        Password
 * @property string  $repeat_password Repeat password
 * @property Profile $profile         Profile
 */
class User extends \common\modules\user\models\User
{
    /**
     * @var string $password Password
     */
    public $password;

    /**
     * @var string $repeat_password Repeat password
     */
    public $repeat_password;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            // Required
            [['username', 'email', 'password', 'repeat_password'], 'required'],

            // Trim
            [['username', 'email', 'password', 'repeat_password'], 'trim'],

            // String
            [['password', 'repeat_password'], 'string', 'min' => 6, 'max' => 30],

            // Unique
            [['username', 'email'], 'unique'],

            // Username
            ['username', 'match', 'pattern' => '/^[a-zA-Z0-9_-]+$/'],
            ['username', 'string', 'min' => 3, 'max' => 30],

            // E-mail
            ['email', 'string', 'max' => 100],
            ['email', 'email'],

            // Repassword
            ['repeat_password', 'compare', 'compareAttribute' => 'password'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            'signup' => ['username', 'email', 'password', 'repeat_password'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        $labels = parent::attributeLabels();

        return array_merge($labels, [
            'password'        => Module::t('user', 'ATTR_PASSWORD'),
            'repeat_password' => Module::t('user', 'ATTR_REPASSWORD'),
        ]);
    }

    /**
     * @inheritdoc
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert))
        {
            if ($this->isNewRecord)
            {
                $this->setPassword($this->password);
            }
            return true;
        }
        return false;
    }

    /**
     * @inheritdoc
     */
    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);

        if ($insert)
        {
            if ($this->profile !== null)
            {
                $this->profile->save(false);
            }

            $auth = Yii::$app->authManager;
            $role = $auth->getRole(self::ROLE_DEFAULT);
            $auth->assign($role, $this->id);

            if (Module::current()->requireEmailConfirmation === true)
            {
                $this->send();
            }
        }
    }

    /**
     * @return Profile|null 用户个人信息
     */
    public function getProfile()
    {
        return $this->hasOne(Profile::className(), ['user_id' => 'id'])->inverseOf('user');
    }

    /**
     * Send an email confirmation token.
     *
     * @return boolean true if email was sent successfully
     */
    public function send()
    {
        return $this->module->mail
            ->compose('signup', ['model' => $this])
            ->setTo($this->email)
            ->setSubject(Module::t('user', 'EMAIL_SUBJECT_SIGNUP') . ' ' . Yii::$app->name)
            ->send();
    }
}
